package com.example.controller;

import com.example.configuration.FileUtils;
import com.example.configuration.SaveFile;
import com.example.pojo.Result;
import com.example.pojo.formListPojo;
import com.example.pojo.pdfDocumentPojo;
import com.example.service.formListService;
import com.example.service.pdfDocumentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.SQLOutput;
import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName:pdfDocumentController
 * Package:com.example.controller
 * Description:
 *
 * @Author：牧星苑
 * @Create 2025/8/14 15:50
 * @Version 1.0
 */
@RestController
@Slf4j
public class pdfDocumentController {

    @Autowired
    private pdfDocumentService pdfDocumentService;
    @Autowired
    private formListService formListService;

    @Value("${file.upload-dir}")
    private String saveDir;

    //插入一条pdf文件
    @PostMapping("/addPdfDocumentToExamineProblem")
    public Result addPdfDocumentToExamineProblem(
            @RequestParam("file") MultipartFile file,
            @RequestParam String number,
            @RequestParam String name,
            @RequestParam String name_english,
            @RequestParam String name_russian,
            @RequestParam String id,
            @RequestParam String document_path,
            @RequestParam String upload_person) {
        try {
            if (!file.isEmpty()) {
                String filePath = saveDir + document_path;
                File dest = new File(filePath);
                // 将文件保存到服务器上的指定位置
                file.transferTo(dest);
                System.out.println("保存成功");
            }
        }
        catch (IOException e) {
            // 捕获异常并返回错误消息
            throw new RuntimeException("文件保存失败：" + e.getMessage());
        }
        // 调用Service保存数据
        String result = pdfDocumentService.addPdfSystemDocument(
                id, name, number, name, name_english, name_russian, document_path, upload_person);

        return Result.success(result);
    }

    //查询所有文件信息
    @PostMapping("/queryPdfDocumentAllToExamineProblem")
    public Result queryPdfDocumentAllToExamineProblem(){
        //调用pdfDocumentService.queryAll()方法获取数据
        //返回结果存储在List<pdfDocumentPojo>类型的pojoList变量中
        List<pdfDocumentPojo> pojoList = pdfDocumentService.queryAll();
        //返回一个成功的 Result 对象，对象为表单中所有数据
        return Result.success(pojoList);
    }

    //更新文档信息
    @PostMapping("/updatePdfDocumentToExamineProblem")
    public Result updatePdfDocumentToExamineProblem(@RequestBody pdfDocumentPojo pojo) {
        System.out.println(pojo);
        String result = pdfDocumentService.updatePdfDocument(pojo);
        return Result.success(result);
    }

    //更新文档信息，同时更新文档附件
    @PostMapping("/updatePdfDocumentWithFileToExamineProblem")
    public Result updatePdfDocumentWithFileToExamineProblem(
            @RequestParam("file") MultipartFile file,
            @RequestParam String id,
            @RequestParam String number,
            @RequestParam String name,
            @RequestParam String name_english,
            @RequestParam String name_russian,
            @RequestParam String version_number,
            @RequestParam String document_path,
            @RequestParam String document_path_new) throws IOException {

        // 先删除旧文件
        //删除文件
        String filePath =saveDir+document_path; // 替换为你的文件路径
        System.out.println(filePath);
        // 使用 Paths 类的静态方法 get 来根据给定的文件路径字符串 filePath 创建一个 Path 对象
        Path path = Paths.get(filePath);
        try {
            Files.delete(path);
            if (!file.isEmpty()) {
                String filePathNew = saveDir + document_path_new;
                File dest = new File(filePathNew);
                // 将文件保存到服务器上的指定位置
                file.transferTo(dest);
                //调用service层修改数据库信息
                pdfDocumentPojo pojo = new pdfDocumentPojo();
                pojo.setId(id);
                pojo.setNumber(number);
                pojo.setName(name);
                pojo.setName_english(name_english);
                pojo.setName_russian(name_russian);
                pojo.setVersion_number(version_number);
                pojo.setDocument_path(document_path_new);
                pojo.setSys_deleted(0);
                pdfDocumentService.updatePdfDocument(pojo);
            }
            return Result.success();

        }
        catch (IOException e) {
            System.err.println("删除文件时出错: " + e.getMessage());
            String filePathNew = saveDir + document_path_new;
            File dest = new File(filePathNew);
            // 将文件保存到服务器上的指定位置
            file.transferTo(dest);
            //调用service层修改数据库信息
            pdfDocumentPojo pojo = new pdfDocumentPojo();
            pojo.setId(id);
            pojo.setNumber(number);
            pojo.setName(name);
            pojo.setName_english(name_english);
            pojo.setName_russian(name_russian);
            pojo.setVersion_number(version_number);
            pojo.setDocument_path(document_path_new);
            pojo.setSys_deleted(0);
            pdfDocumentService.updatePdfDocument(pojo);
            return Result.success("删除文件时出错");
        }

    }

    //新增一条表单
    @PostMapping("/addFormDocumentToExamineProblem")
    public Result addFormDocumentToExamineProblem(
            @RequestParam("file") MultipartFile file,
            @RequestParam String number,
            @RequestParam String name,
            @RequestParam String name_english,
            @RequestParam String name_russian,
            @RequestParam String id,
            @RequestParam String document_path,
            @RequestParam String upload_person) {
        try {
            if (!file.isEmpty()) {
                String filePath = saveDir + document_path;
                File dest = new File(filePath);
                // 将文件保存到服务器上的指定位置
                file.transferTo(dest);
                System.out.println("保存成功");
            }
        }
        catch (IOException e) {
            // 捕获异常并返回错误消息
            throw new RuntimeException("文件保存失败：" + e.getMessage());
        }
        // 调用Service保存数据
        String result = formListService.addFormSystemDocument(
                id, name, number, name, name_english, name_russian, document_path, upload_person);

        return Result.success(result);
    }

    //查询所有表单文件信息
    @PostMapping("/queryFormDocumentAllToExamineProblem")
    public Result queryFormDocumentAllToExamineProblem(){
        //调用formListService.queryAll()方法获取数据
        //返回结果存储在List<formListPojo>类型的pojoList变量中
        List<formListPojo> pojoList = formListService.queryAll();
        //返回一个成功的 Result 对象，对象为表单中所有数据
        return Result.success(pojoList);
    }

    //更新表单信息
    @PostMapping("/updateFormDocumentToExamineProblem")
    public Result updateFormDocumentToExamineProblem(@RequestBody formListPojo pojo) {
        System.out.println(pojo);
        String result = formListService.updateFormDocument(pojo);
        return Result.success(result);
    }


    //更新表单信息，同时更新表单附件
    @PostMapping("/updateFormDocumentWithFileToExamineProblem")
    public Result updateFormDocumentWithFileToExamineProblem(
            @RequestParam("file") MultipartFile file,
            @RequestParam String id,
            @RequestParam String number,
            @RequestParam String name,
            @RequestParam String name_english,
            @RequestParam String name_russian,
            @RequestParam String version_number,
            @RequestParam String document_path,
            @RequestParam String document_path_new) throws IOException {

        // 先删除旧文件
        //删除文件
        String filePath =saveDir+document_path; // 替换为你的文件路径
        System.out.println(filePath);
        // 使用 Paths 类的静态方法 get 来根据给定的文件路径字符串 filePath 创建一个 Path 对象
        Path path = Paths.get(filePath);
        try {
            Files.delete(path);
            if (!file.isEmpty()) {
                String filePathNew = saveDir + document_path_new;
                System.out.println(filePathNew);
                File dest = new File(filePathNew);
                // 将文件保存到服务器上的指定位置
                file.transferTo(dest);
                //调用service层修改数据库信息
                formListPojo pojo = new formListPojo();
                pojo.setId(id);
                pojo.setNumber(number);
                pojo.setName(name);
                pojo.setName_english(name_english);
                pojo.setName_russian(name_russian);
                pojo.setVersion_number(version_number);
                pojo.setDocument_path(document_path_new);
                pojo.setSys_deleted(0);
                formListService.updateFormDocument(pojo);
            }
            return Result.success();

        }
        catch (IOException e) {
            System.err.println("删除文件时出错: " + e.getMessage());
            String filePathNew = saveDir + document_path_new;
            System.out.println(filePathNew);
            File dest = new File(filePathNew);
            // 将文件保存到服务器上的指定位置
            file.transferTo(dest);
            //调用service层修改数据库信息
            formListPojo pojo = new formListPojo();
            pojo.setId(id);
            pojo.setNumber(number);
            pojo.setName(name);
            pojo.setName_english(name_english);
            pojo.setName_russian(name_russian);
            pojo.setVersion_number(version_number);
            pojo.setDocument_path(document_path_new);
            pojo.setSys_deleted(0);
            formListService.updateFormDocument(pojo);
            return Result.success("删除文件时出错");
        }
    }

    //下载表单
    @GetMapping("/downloadFormToExamineProblem")
    public Result downloadFormToExamineProblem(@RequestParam("fileName") String fileName){
        byte[] file= formListService.readFile(fileName);
        System.out.println(fileName);
        return Result.success(file);
    }
}